package com.xbn.xbnmall.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xbn.common.exception.*;
import com.xbn.common.to.MemberRegistTo;
import com.xbn.xbnmall.member.feign.CouponFeignService;
import com.xbn.xbnmall.member.vo.UserLoginVo;
import com.xbn.xbnmall.member.vo.WeiboUserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xbn.xbnmall.member.entity.MemberEntity;
import com.xbn.xbnmall.member.service.MemberService;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.R;



/**
 * 会员
 *
 * @author XBN
 * @email 78742541@qq.com
 * @date 2020-06-27 13:45:57
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeignService couponFeignService;

    /*
    * 社交登陆(微博)
    * */
    @PostMapping("/weibo/oauth2/login")
    public R login(@RequestBody WeiboUserToken weiboUserToken) throws Exception {
        MemberEntity memberEntity=  memberService.login(weiboUserToken);
        if(memberEntity!=null)
        {
            return R.ok().put("data",memberEntity);
        }
        else {
            return R.error(BizCodeEnume.Third_LOGIN_ERROR.getCode(),
                    BizCodeEnume.Third_LOGIN_ERROR.getMsg());
        }
    }
    /*
    * 登陆
    * */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo){
        MemberEntity memberEntity=null;
        try {
             memberEntity=  memberService.login(userLoginVo);
        }catch (UserNotFindException e)
        {
            return R.error(BizCodeEnume.USER_NOT_FIND_EXCEPTION.getCode(),
                    BizCodeEnume.USER_NOT_FIND_EXCEPTION.getMsg());
        }
        catch (PasswordErrorException e)
        {
            return R.error(BizCodeEnume.PASS_ERROR_EXCEPTION.getCode(),
                    BizCodeEnume.PASS_ERROR_EXCEPTION.getMsg());
        }
        return R.ok().put("data",memberEntity);

    }
    /*
    * 注册
    * */
        @PostMapping("/regist")
      public R regist(@RequestBody MemberRegistTo memberRegistTo){
            try {
                memberService.regist(memberRegistTo);
            }catch (UsernameExistException e)
            {
                return      R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode()
            ,BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
            }
            catch (PhoneExistException e)
            {
      return           R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode()
                        ,BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
            }
            return R.ok();

      }

    /*
    * 查询某个用户的优惠券
    * 连接至优惠券服务器
    * */
    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity=new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
 //   @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
